package com.ycfmedical.bed.ui;

import com.tencent.TIMManager;
import com.tencent.TIMUserStatusListener;
import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.base.BaseMenuActivity;
import com.ycfmedical.bed.bean.FriendshipInfo;
import com.ycfmedical.bed.bean.ImUser;
import com.ycfmedical.bed.business.TlsBusiness;
import com.ycfmedical.bed.event.MessageEvent;
import com.ycfmedical.bed.local.DataManager;
import com.ycfmedical.bed.local.entity.User;
import com.ycfmedical.bed.ui.fragment.ContactFragment;
import com.ycfmedical.bed.ui.fragment.ConversationFragment;
import com.ycfmedical.bed.utils.ResourceUtil;
import com.ycfmedical.bed.utils.ShareUtil;
import com.ycfmedical.bed.widgets.DialogIosAlert;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.router.Router;

/**
 * Created by Egoist on 2017/3/10.
 */

public class ImActivity extends BaseMenuActivity {


    @BindView(android.R.id.tabhost)
    FragmentTabHost mTabHost;

    ImageView icon;
    TextView title;
    private LayoutInflater layoutInflater;
    private ImageView msgUnread;

    private final Class fragmentArray[] = {ConversationFragment.class, ContactFragment.class};
    private int mTitleArray[] = {R.string.home_conversation_tab, R.string.home_contact_tab};
    private int mImageViewArray[] = {R.drawable.tab_conversation, R.drawable.tab_contact};
    private String mTextviewArray[] = {"contact", "conversation"};

    public static void launch(Activity activity)
    {
        Router.newIntent(activity).to(ImActivity.class).launch();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_imcontainer;
    }

    @Override
    public Object newP() {
        return null;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initTabHost();
        initImLogic();//IM互踢下线逻辑
    }

    private void initTabHost() {
        layoutInflater = LayoutInflater.from(this);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.contentPanel);
        int fragmentCount = fragmentArray.length;
        for (int i = 0; i < fragmentCount; ++i) {
            //为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
            //将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
            mTabHost.getTabWidget().setDividerDrawable(null);

        }
    }

    private View getTabItemView(int index) {
        View view = layoutInflater.inflate(R.layout.layout_im_tabhost, null);
        icon = (ImageView) view.findViewById(R.id.icon);
        title = (TextView) view.findViewById(R.id.title);
        icon.setImageResource(mImageViewArray[index]);
        title.setText(mTitleArray[index]);
        if (index == 0) {
            msgUnread = (ImageView) view.findViewById(R.id.tabUnread);
        }
        return view;
    }


    /**
     * 互踢下线逻辑
     */
    private void initImLogic() {
        TIMManager.getInstance().setUserStatusListener(new TIMUserStatusListener() {
            @Override
            public void onForceOffline() {
                XLog.d(getTag(), "receive force offline temoerature_item");
//                new DialogIosAlert(ImActivity.this).builder()
//                        .setTitle(ResourceUtil.getResources().getString(R.string.offline_inform))
//                        .setCancelable(true)
//                        .setCanceledOnTouchOutside(true)
//                        .setMsg("用户票据标识过期，需要重新登录!\n" + "使用标识是为了防止用户信息被窃取")
//                        .setNegativeButton(ResourceUtil.getResources().getString(R.string.confirm), v ->
//                        {
//                            _logout();
//                        })
//                        .show();
            }

            @Override
            public void onUserSigExpired() {
                //票据过期，需要重新登录
//                new DialogIosAlert(ImActivity.this).builder()
//                        .setTitle(ResourceUtil.getResources().getString(R.string.offline_inform))
//                        .setCancelable(true)
//                        .setCanceledOnTouchOutside(true)
//                        .setMsg(getString(R.string.tls_expire))
//                        .setNegativeButton(ResourceUtil.getResources().getString(R.string.confirm), v ->
//                        {
//                            _logout();
//                        })
//                        .show();
            }
        });
    }

    private void _logout(){
        TlsBusiness.logout(ImUser.getInstance().getImAccount());
        ImUser.getInstance().setImAccount(null);
        MessageEvent.getInstance().clear();
        FriendshipInfo.getInstance().clear();
        SplashActivity.launch(this);
        finish();

    }

    public static void imLogout() {
        String imAccount = ImUser.getInstance().getImAccount();
        if (!Kits.Empty.check(ImUser.getInstance().getImAccount())) {
            TlsBusiness.logout(ImUser.getInstance().getImAccount());
            MessageEvent.getInstance().clear();
            FriendshipInfo.getInstance().clear();
            DataManager.getInstance().setUser(new User());
            ImUser.getInstance().setImAccount("");
            ImUser.getInstance().setImSign("");
        }else {
            // 无外网情况 也要让当前用户退出
            DataManager.getInstance().setUser(new User());
            ImUser.getInstance().setImAccount("");
        }
    }

    /**
     * 设置未读tab显示
     */
    public void setMsgUnread(boolean noUnread) {
        msgUnread.setVisibility(noUnread ? View.GONE : View.VISIBLE);
    }
}
